package hz.mobileproject.pda.broadcast;

import android.content.Context;
import android.content.IntentFilter;
import android.device.ScanManager;
import android.device.scanner.configuration.Triggering;


/**
 * Created by wangliang on 2016/11/16.
 */

public class HandleBroadcast {


    private final ScanBroadcastReceiver broadcast;
    private IShowBarcodeStr iShowView;
    private Context context;

    private final static String SCAN_ACTION = "urovo.rcv.message";//扫描结束action
    private ScanManager mScanManager;

    public HandleBroadcast(final IShowBarcodeStr iShowView, Context context) {
        this.iShowView = iShowView;
        this.context = context;

        //必须动态注册才能实现回调
         broadcast = new ScanBroadcastReceiver(iShowView);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(SCAN_ACTION);
        context.registerReceiver(broadcast, intentFilter);

    }


    public void sendMyBroadcast() throws Error{

            mScanManager = new ScanManager();
            mScanManager.openScanner();
            mScanManager.switchOutputMode( 0);
            mScanManager.setTriggerMode(Triggering.CONTINUOUS);
            mScanManager.startDecode();
    }

    public void overBroadcastReceiver(Context context){
        context.unregisterReceiver(broadcast);
        if (mScanManager!=null){
            mScanManager.stopDecode();
//        mScanManager.closeScanner();
        }

    }



}
